package com.dcivision.dms.client;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Hashtable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.dms.client.parser.IndexParser;

// Referenced classes of package com.dci.dms.util.client:
// PropertyGetter, HttpSender, HtmlFormText, HtmlFormFile
public class DmsDocumentUploadTracker extends Thread {

  public static final String REVISION = "$Revision: 1.44.2.2 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());
  private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  private static Object lock = new Object();
  private Hashtable hConfTable = null;
  private static java.util.Properties props = new java.util.Properties();
  private static final String separator = ".";
  private String sScanMethod;
  private String sUploadFileExten;
  private String sActionStatus;

  //Get the login user name and pwd if provided
  private String loginName = "";
  private String loginPwd = "";
  private String rootID = "";
  private String parentID = "";

  public DmsDocumentUploadTracker(Hashtable confTable, int pos) {
    hConfTable = confTable;
    Integer position = (Integer) hConfTable.get("position");
  }

  public void run() {
    Integer position = (Integer) hConfTable.get("position");
    String srcDir = (String) hConfTable.get(IndexParser.SOURCE_DIR);

    String strList[][] = null;
    String timeInterval = (String)hConfTable.get(IndexParser.INTERVAL);
    String uploadInterval = (String)hConfTable.get(IndexParser.UPLOAD_INTERVAL);    
    sScanMethod =(String)hConfTable.get(IndexParser.SCAN_METHOD);
    if (timeInterval != null) {
      Integer interval = new Integer(timeInterval.trim());
      int sleepingInterval = interval.intValue() * 1000;
      int uploadSleepInterval = 0;
      
      if(uploadInterval!=null){
        uploadSleepInterval = Integer.parseInt(uploadInterval.trim()) * 1000;
      }
      
      int gcFileInterval = 10;
      int gcFileCount = 0;
      do {
        synchronized (lock) {
        try {
        String sourceDir = (String)hConfTable.get(IndexParser.SOURCE_DIR);
        File sourceDirectory = new File(sourceDir);
        File listFileSource[] = sourceDirectory.listFiles();
            if (listFileSource != null) {
          for(int i = 0; i < listFileSource.length; i++) {
            log.debug("Scanning File:" + listFileSource[i].getAbsolutePath());
                if (listFileSource[i].isFile() ) {
              /*
              String uploadFilePath = listFileSource[i].getAbsolutePath();
              hConfTable.put(IndexParser.UPLOAD_FILE_PATH, uploadFilePath);
              hConfTable.put(IndexParser.UPLOAD_FILE_FILENAME, listFileSource[i].getName());

              String fullTextFilePath = String.valueOf(listFileSource[i].getAbsolutePath().substring(0, listFileSource[i].getAbsolutePath().length() - 4)) + String.valueOf((String)hConfTable.get(IndexParser.SCAN_FULL_TEXT_EXTEN));
              hConfTable.put(IndexParser.FULL_TEXT_FILE_PATH, fullTextFilePath);

              //String content = getFullTextFile(fullTextFilePath);
              String content = "";    //disabled since will upload the entire file stream into FormFile
              hConfTable.put(IndexParser.FULL_TEXT, content);

              String targetFile = listFileSource[i].getAbsolutePath();
              GlobalIndexTransformation indexTransformation = new GlobalIndexTransformation(sScanMethod);
              indexTransformation.IndexTransformer(targetFile, hConfTable);

              //Configure the login name and password if entered
              loginName = indexTransformation.getLoginName();
              loginPwd = indexTransformation.getLoginPwd();
              rootID = indexTransformation.getRootID();
              parentID = indexTransformation.getParentID();

              httpUploadFile(listFileSource[i]);
              indexTransformation.backupAllFiles(sActionStatus);
              */
                  if (isUploadFile(listFileSource[i])) {
                uploadFile(listFileSource[i], null);
                    if(uploadSleepInterval>0){                    
                      Thread.sleep(uploadSleepInterval);
                    }
              }
                } else if(listFileSource[i].isDirectory()) {
                  log.debug("folder name:" + listFileSource[i].getName());
                  if("Y".equals(hConfTable.get(IndexParser.SUPPORT_FOLDER_STRUCTURE))) {
                processFolder(listFileSource[i], (hConfTable.get(IndexParser.DMS_PARENT_ID).toString()));
              }
            }
          }
        }
            if (gcFileInterval == gcFileCount) {
              log.debug("Memory Clean-up to free resources...");
              System.gc();
              gcFileCount = 0;
            }
            gcFileCount++;
        Thread.sleep(sleepingInterval);
          } catch(InterruptedException ie) {
            log.error(ie, ie);
      } catch(Exception e){
        log.error(e, e);
      }
        }
      } while(true);
    }
  }

  protected void processFolder(File sourceDirectory, String parentFolderID) {
    String currentFolderID = httpUploadFolder(sourceDirectory.getName(), parentFolderID);
    File[] listFileSource = sourceDirectory.listFiles();

    if (listFileSource != null) {
      for (int i = 0; i < listFileSource.length; i++) {
        if (listFileSource[i].isFile()) {
          if (isUploadFile(listFileSource[i])) {
            uploadFile(listFileSource[i], currentFolderID);
          }
        } else {
          String subFolderName = listFileSource[i].getName();

          //Integer folderID = getFolderID(parentFolderID, subFolderName);
          if((listFileSource[i].exists()) && (listFileSource[i].isDirectory())){
            processFolder(listFileSource[i], currentFolderID);
          }
        }
      }
    }
  }

  public String httpUploadFolder(String folderName, String parentID) {
      String[][] strList = null;
      StringBuffer returnMessage = null;
      String currentFolderID = "";
    HttpSender httpSender = new HttpSender();
      try {
        httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_NAME,
            (String) hConfTable.get(IndexParser.LOGIN_NAME)));
        httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_PASSWORD,
            (String) hConfTable.get(IndexParser.LOGIN_PASSWORD)));
        httpSender.addHtmlFormText(new HtmlFormText("creatorID", "0"));
        httpSender.addHtmlFormText(new HtmlFormText("rootID", (String) hConfTable.get(IndexParser.DMS_ROOT_ID)));
        httpSender.addHtmlFormText(new HtmlFormText("parentID", parentID.toString()));
        httpSender.addHtmlFormText(new HtmlFormText("documentName", folderName));
        httpSender.addHtmlFormText(new HtmlFormText("createType", "U"));
        httpSender.addHtmlFormText(new HtmlFormText("documentType", "Z"));
        httpSender.addHtmlFormText(new HtmlFormText("opMode", "INS"));
        httpSender.addHtmlFormText(new HtmlFormText("navMode", "N"));
        httpSender.addHtmlFormText(new HtmlFormText("returnTo","SCAN"));

        //httpSender.addHtmlFormFile(new HtmlFormFile("uploadFile",
        // file.getAbsolutePath()));
        httpSender.addHtmlFormText(new HtmlFormText("updateFailedAttemptFlag", "N"));

        httpSender.setSubmissionURL((String) hConfTable.get(IndexParser.URL));

        //transmit the action and get the message return
       returnMessage = httpSender.transmit();

        logMessage(returnMessage);
        sActionStatus = httpSender.getActionStatus();
        if((returnMessage.indexOf("CREATE_FOLDER_ID:")>0) && (returnMessage.indexOf("~")>0)) {
          currentFolderID=returnMessage.substring(returnMessage.indexOf("CREATE_FOLDER_ID:")+"CREATE_FOLDER_ID:".length(),returnMessage.indexOf("~"));
        }
        return currentFolderID;

        //return new Integer(returnMessage.toString());
      } catch (InterruptedException ie) {
        log.error(ie, ie);
        return currentFolderID;
      } catch (Exception e) {
        log.error(e, e);
        return currentFolderID;
    } finally {
      httpSender = null;
      }
    }

  protected void uploadFile(File uploadFile, String parentFolderID) {
    GlobalIndexTransformation indexTransformation = null;
    String signatiureTxtFile=String.valueOf(uploadFile.getAbsolutePath().substring(0, uploadFile.getAbsolutePath().length() - 4)) +"_s"+ String.valueOf(hConfTable.get(IndexParser.SCAN_FULL_TEXT_EXTEN));
    String signatureFilePath = String.valueOf(uploadFile.getAbsolutePath().substring(0, uploadFile.getAbsolutePath().length() - 4)) +"_s" +String.valueOf(uploadFile.getAbsolutePath().substring(uploadFile.getAbsolutePath().length() - 4, uploadFile.getAbsolutePath().length()));
    try {
    String uploadFilePath = uploadFile.getAbsolutePath();
       hConfTable.put(IndexParser.UPLOAD_FILE_PATH, uploadFilePath);
       hConfTable.put(IndexParser.UPLOAD_FILE_FILENAME, uploadFile.getName());

       String fullTextFilePath = String.valueOf(uploadFile.getAbsolutePath().substring(0, uploadFile.getAbsolutePath().length() - 4)) + String.valueOf(hConfTable.get(IndexParser.SCAN_FULL_TEXT_EXTEN));
       hConfTable.put(IndexParser.FULL_TEXT_FILE_PATH, fullTextFilePath);
       hConfTable.put(IndexParser.SIGNATURE_PATH, signatureFilePath);

       //String content = getFullTextFile(fullTextFilePath);
       String content = "";    //disabled since will upload the entire file stream into FormFile
       hConfTable.put(IndexParser.FULL_TEXT, content);

       String targetFile = uploadFile.getAbsolutePath();
       log.debug("\ntarget file = \t\t" + targetFile);
      indexTransformation = new GlobalIndexTransformation(sScanMethod);
       indexTransformation.IndexTransformer(targetFile, hConfTable);

       //Configure the login name and password if entered
       loginName = indexTransformation.getLoginName();
       loginPwd = indexTransformation.getLoginPwd();
       rootID = indexTransformation.getRootID();
       parentID = indexTransformation.getParentID();

       httpUploadFile(uploadFile,parentFolderID);
       indexTransformation.backupAllFiles(sActionStatus);
      if (GlobalIndexTransformation.STATUS_ERROR.equals(sActionStatus)) {
        copySignatureFile(signatureFilePath,(String)hConfTable.get(IndexParser.ERROR_DIR));
        copySignatureFile(signatiureTxtFile,(String)hConfTable.get(IndexParser.ERROR_DIR));
      }else{
       copySignatureFile(signatureFilePath,(String)hConfTable.get(IndexParser.BACKUP_DIR));
       copySignatureFile(signatiureTxtFile,(String)hConfTable.get(IndexParser.BACKUP_DIR));
      }
    } catch(Exception e) {
      indexTransformation.backupAllFiles(GlobalIndexTransformation.STATUS_ERROR);
      copySignatureFile(signatureFilePath,(String)hConfTable.get(IndexParser.ERROR_DIR));
      copySignatureFile(signatiureTxtFile,(String)hConfTable.get(IndexParser.ERROR_DIR));
    }
  }

  public boolean isUploadFile(File file) {
    boolean result = false;
    Integer position = (Integer) hConfTable.get("position");
    sUploadFileExten = (String) hConfTable.get(IndexParser.SCAN_UPLOAD_FILE_EXTEN);
    String uploadFileIndexExten = (String) hConfTable.get(IndexParser.SCAN_UPLOAD_FILE_INDEX_EXTEN);
    boolean getTargetUploadFile = file.getName().toLowerCase().endsWith(sUploadFileExten.toLowerCase());

    String scanIndexExten = (String)hConfTable.get(IndexParser.SCAN_DOCUMENT_INDEX_EXTEN);
    String scanIndexFilePath = String.valueOf(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4)) + String.valueOf(scanIndexExten);
    File scanIndexFile = new File(scanIndexFilePath);
    boolean getScanIndexFile = scanIndexFile.exists() && scanIndexFile.canWrite() && scanIndexFile.canRead() && scanIndexFile.renameTo(scanIndexFile);

    if ("".equals(sUploadFileExten) || sUploadFileExten == null || "".equals(uploadFileIndexExten) || uploadFileIndexExten==null) {
      return true;
    } else {
      if ("Y".equals(hConfTable.get(IndexParser.SCAN_FULL_TEXT_ENABLE))) {
        String fullTextExten = (String)hConfTable.get(IndexParser.SCAN_FULL_TEXT_EXTEN);
        String fullTextFilePath = String.valueOf(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4)) + String.valueOf(fullTextExten);
        File fullTextFile = new File(fullTextFilePath);
        boolean getFullTextFile = false;

        getFullTextFile = fullTextFile.exists() && fullTextFile.canWrite() && fullTextFile.canRead() && fullTextFile.renameTo(fullTextFile);
        result = getTargetUploadFile && getScanIndexFile && getFullTextFile;
      } else {
        result = getTargetUploadFile && getScanIndexFile;
      }
      return result;
    }
  }

  public String getFullTextFile(String fullTextPath) {
    String content = "";
    return content;

  }

  public void httpUploadFile(File file, String parentFolderID){
    String strList[][] = null;
    HttpSender httpSender = new HttpSender();
    httpSender.setSubmissionURL((String)hConfTable.get(IndexParser.URL));
    Integer position = (Integer) hConfTable.get("position");

    // add by roy
    String uploadRootID = rootID;
    String uploadParentID = null;
    if (parentFolderID !=null){
      uploadParentID = parentFolderID;
    }else{
      uploadParentID = parentID;
    }
    //end of add by roy

    try {
      httpSender.setTargetFile(file.getAbsolutePath());
      if ("BIZSOFT".equals(hConfTable.get(IndexParser.SCAN_METHOD))) {
        if (loginName != null && !"".equals(loginName)) {
          httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_NAME, loginName));
          httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_PASSWORD, loginPwd));
          httpSender.addHtmlFormText(new HtmlFormText("creatorID", "0"));
        } else {
          httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_NAME, (String)hConfTable.get(IndexParser.LOGIN_NAME)));
          httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_PASSWORD, (String)hConfTable.get(IndexParser.LOGIN_PASSWORD)));
          httpSender.addHtmlFormText(new HtmlFormText("creatorID", "0"));
        }

        if (uploadRootID != null && !"".equals(uploadRootID)) {
          httpSender.addHtmlFormText(new HtmlFormText("rootID", uploadRootID));
        } else {
          httpSender.addHtmlFormText(new HtmlFormText("rootID", (String)hConfTable.get(IndexParser.DMS_ROOT_ID)));
        }
        if (uploadParentID != null && !"".equals(uploadParentID)) {
          httpSender.addHtmlFormText(new HtmlFormText("parentID", uploadParentID));
        } else {
          httpSender.addHtmlFormText(new HtmlFormText("parentID", (String)hConfTable.get(IndexParser.DMS_PARENT_ID)));
        }
      } else {
        httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_NAME, (String)hConfTable.get(IndexParser.LOGIN_NAME)));
        httpSender.addHtmlFormText(new HtmlFormText(IndexParser.LOGIN_PASSWORD, (String)hConfTable.get(IndexParser.LOGIN_PASSWORD)));
        httpSender.addHtmlFormText(new HtmlFormText("creatorID", "0"));
        httpSender.addHtmlFormText(new HtmlFormText("rootID", (String)hConfTable.get(IndexParser.DMS_ROOT_ID)));
        if (uploadParentID != null && !"".equals(uploadParentID)) {
          httpSender.addHtmlFormText(new HtmlFormText("parentID", uploadParentID));
        } else {
          httpSender.addHtmlFormText(new HtmlFormText("parentID", (String)hConfTable.get(IndexParser.DMS_PARENT_ID)));
        }
      }
      httpSender.addHtmlFormText(new HtmlFormText("createType", "U"));
      if (!IndexParser.ANY_FILE_TYPE.equals(sUploadFileExten)) {
        httpSender.addHtmlFormText(new HtmlFormText("trueFileName", sUploadFileExten));
      } else {
        String fileExt ="";
        int p = file.getName().lastIndexOf(".");
        fileExt = file.getName().substring(p,file.getName().length());
        httpSender.addHtmlFormText(new HtmlFormText("trueFileName", fileExt));
      }
      httpSender.addHtmlFormText(new HtmlFormText("documentType", (String)hConfTable.get(IndexParser.DMS_DOCUMENT_TYPE)));
      httpSender.addHtmlFormText(new HtmlFormText("opMode", "INS"));
      httpSender.addHtmlFormText(new HtmlFormText("navMode", "N"));
      httpSender.addHtmlFormFile(new HtmlFormFile("uploadFile", file.getAbsolutePath()));
      httpSender.addHtmlFormText(new HtmlFormText("updateFailedAttemptFlag", "N"));   //used to avoid the update count value
      if ("Y".equals(hConfTable.get(IndexParser.SUPPORT_FOLDER_STRUCTURE))) {
        httpSender.addHtmlFormText(new HtmlFormText("scanSupportFolderStructure", "Y"));
      }
      /* dci-vision scanning index file upload*/
      String scanIndexExten = (String)hConfTable.get(IndexParser.SCAN_DOCUMENT_INDEX_EXTEN);
      String scanIndexFilePath ="";
      scanIndexFilePath = String.valueOf(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4)) + "dci" + String.valueOf(scanIndexExten);

      File scanIndexFile = new File(scanIndexFilePath);

      if (scanIndexFile.exists()) {
        httpSender.addHtmlFormFile(new HtmlFormFile("scanFile", scanIndexFile.getAbsolutePath()));
      }

      /* dci-vision fulltext file upload*/
      String fullTextExten = (String)hConfTable.get(IndexParser.SCAN_FULL_TEXT_EXTEN);
      String fullTextFilePath ="";
      fullTextFilePath = String.valueOf(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4)) + String.valueOf(fullTextExten);
      File fullTextFile = new File(fullTextFilePath);
      if (fullTextFile.exists()) {
        httpSender.addHtmlFormFile(new HtmlFormFile("fullTextFile", fullTextFile.getAbsolutePath()));
      }

      /* demo purpose on 28 Feb 05 begin*/
      String enableSignature = (String)hConfTable.get(IndexParser.ENABLE_SIGNATURE_UPLOAD) ;
      if ("Y".equals(enableSignature)||"y".equals(enableSignature)) {
        String signatureFilePath ="";
        signatureFilePath = String.valueOf(file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4)) +"_s" +String.valueOf(file.getAbsolutePath().substring(file.getAbsolutePath().length() - 4, file.getAbsolutePath().length()));
        File signatureFile = new File(signatureFilePath);
        if (signatureFile.exists()) {
          httpSender.addHtmlFormFile(new HtmlFormFile("signatureFile", signatureFile.getAbsolutePath()));
        }
      }
      /* demo purpose on 28 Feb 05 end*/

      //transmit the action and get the message return
      StringBuffer returnMessage = httpSender.transmit();
      logMessage(returnMessage);
      sActionStatus = httpSender.getActionStatus();
    } catch(InterruptedException ie){
      log.error(ie, ie);
    } catch(Exception e) {
      log.error(e, e);
    }
  }

  public void logMessage(StringBuffer returnMessage) {
    try {
      String logDir = (String)hConfTable.get(IndexParser.LOG_DIR);
      File logFolder = new File(logDir);
      if (!logFolder.exists()) {
        logFolder.mkdirs();
      }
      if (returnMessage.lastIndexOf(GlobalIndexTransformation.STATUS_ERROR)>0) {
        String logFilePath = logDir.concat("/").concat("failLog"+sdf.format(new java.util.Date())+".txt");
        File logFile = new File(logFilePath);
        FileWriter fo = null;
        if (logFile.exists()) {
          fo = new FileWriter(logFile, true);
        } else {
          fo = new FileWriter(logFile);
        }
        fo.write(replaceString(returnMessage.toString(), "\n", "\r\n"));
        fo.write("\r\n");
        fo.close();
        fo = null;
      } else if (returnMessage.lastIndexOf("system")>0) {
        String logFilePath = logDir.concat("/").concat("sucessfulLog"+sdf.format(new java.util.Date())+".txt");
        File logFile = new File(logFilePath);
        FileWriter fo = null;
        if (logFile.exists()) {
          fo = new FileWriter(logFile, true);
        } else {
          fo = new FileWriter(logFile);
        }
        fo.write(replaceString(returnMessage.toString(), "\n", "\r\n"));
        fo.write("\r\n");
        fo.close();
        fo = null;
      }
    } catch(Exception e) {
      log.error(e, e);
    }
  }

  public static void main(String args[]) throws Exception  {
    // Read config
    Hashtable[] aryConfTable = configInit();
    int numInstant = aryConfTable.length;

    //Create multiple instant
    DmsDocumentUploadTracker[] cat = new DmsDocumentUploadTracker[numInstant];
    for (int i=0; i<numInstant; i++) {
      Hashtable tmpTable = aryConfTable[i];
      cat[i] = new DmsDocumentUploadTracker(tmpTable, i);
      cat[i].start();
      Thread.sleep(25);
    }
  }

 /** configInit method
 * @return Value of total number of locations
 */
  public static Hashtable[] configInit() throws Exception {
    File config = new File(System.getProperty("DMS_HOME") + "/dms.conf");
    java.io.FileInputStream fis = new java.io.FileInputStream(config);
    props.load(fis);
    //check for multiple locations vs single location
    Integer numInstant = new Integer(props.getProperty(IndexParser.MULTIPLE_INSTANCE));
    Hashtable[] tmpConfTable = new Hashtable[numInstant.intValue()];
    for (int i = 0; i < numInstant.intValue(); i++) {
      Hashtable tmpTable = new Hashtable();
      tmpTable.put("position", new Integer(i));
      tmpTable.put(IndexParser.URL, getConfigProperty(IndexParser.URL, i));
      tmpTable.put(IndexParser.LOGIN_NAME, getConfigProperty(IndexParser.LOGIN_NAME, i));
      tmpTable.put(IndexParser.LOGIN_PASSWORD, getConfigProperty(IndexParser.LOGIN_PASSWORD, i));
      tmpTable.put(IndexParser.INTERVAL, getConfigProperty(IndexParser.INTERVAL, i));
      tmpTable.put(IndexParser.UPLOAD_INTERVAL, getConfigProperty(IndexParser.UPLOAD_INTERVAL, i));
      tmpTable.put(IndexParser.SOURCE_DIR, getConfigProperty(IndexParser.SOURCE_DIR, i));
      tmpTable.put(IndexParser.BACKUP_DIR, getConfigProperty(IndexParser.BACKUP_DIR, i));
      tmpTable.put(IndexParser.ERROR_DIR, getConfigProperty(IndexParser.ERROR_DIR, i));
      tmpTable.put(IndexParser.LOG_DIR, getConfigProperty(IndexParser.LOG_DIR, i));
      tmpTable.put(IndexParser.SCAN_METHOD, getConfigProperty(IndexParser.SCAN_METHOD, i));
      tmpTable.put(IndexParser.SCAN_UPLOAD_FILE_EXTEN, getConfigProperty(IndexParser.SCAN_UPLOAD_FILE_EXTEN, i));
      tmpTable.put(IndexParser.SCAN_DOCUMENT_INDEX_EXTEN, getConfigProperty(IndexParser.SCAN_DOCUMENT_INDEX_EXTEN, i));
      tmpTable.put(IndexParser.SCAN_UPLOAD_FILE_INDEX_EXTEN, getConfigProperty(IndexParser.SCAN_UPLOAD_FILE_INDEX_EXTEN, i));
      tmpTable.put(IndexParser.SCAN_FULL_TEXT_EXTEN, getConfigProperty(IndexParser.SCAN_FULL_TEXT_EXTEN, i));
      tmpTable.put(IndexParser.SCAN_FULL_TEXT_ENABLE, getConfigProperty(IndexParser.SCAN_FULL_TEXT_ENABLE, i));
      if(getConfigProperty(IndexParser.SUPPORT_FOLDER_STRUCTURE, i)!=null){
        tmpTable.put(IndexParser.SUPPORT_FOLDER_STRUCTURE, getConfigProperty(IndexParser.SUPPORT_FOLDER_STRUCTURE, i));
      }else{
        tmpTable.put(IndexParser.SUPPORT_FOLDER_STRUCTURE,"");
      }

      /* demo purpose on 28 Feb 05 begin*/
      if(getConfigProperty(IndexParser.ENABLE_SIGNATURE_UPLOAD, i)!=null){
        tmpTable.put(IndexParser.ENABLE_SIGNATURE_UPLOAD, getConfigProperty(IndexParser.ENABLE_SIGNATURE_UPLOAD, i));
      }else{
        tmpTable.put(IndexParser.ENABLE_SIGNATURE_UPLOAD,"");
      }
      /* demo purpose on 28 Feb 05 end*/

      tmpTable.put(IndexParser.DMS_ROOT_ID, getConfigProperty(IndexParser.DMS_ROOT_ID, i));
      tmpTable.put(IndexParser.DMS_PARENT_ID, getConfigProperty(IndexParser.DMS_PARENT_ID, i));
      tmpTable.put(IndexParser.DMS_DOCUMENT_TYPE, getConfigProperty(IndexParser.DMS_DOCUMENT_TYPE, i));
      tmpTable.put(IndexParser.FIELD_NO_UDF, getConfigProperty(IndexParser.FIELD_NO_UDF, i));
      tmpTable.put(IndexParser.FIELD_DESCRIPTION, getConfigProperty(IndexParser.FIELD_DESCRIPTION, i));
      tmpTable.put(IndexParser.FIELD_REFERENCE_NO, getConfigProperty(IndexParser.FIELD_REFERENCE_NO, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_1, getConfigProperty(IndexParser.FIELD_USER_DEF_1, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_2, getConfigProperty(IndexParser.FIELD_USER_DEF_2, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_3, getConfigProperty(IndexParser.FIELD_USER_DEF_3, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_4, getConfigProperty(IndexParser.FIELD_USER_DEF_4, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_5, getConfigProperty(IndexParser.FIELD_USER_DEF_5, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_6, getConfigProperty(IndexParser.FIELD_USER_DEF_6, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_7, getConfigProperty(IndexParser.FIELD_USER_DEF_7, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_8, getConfigProperty(IndexParser.FIELD_USER_DEF_8, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_9, getConfigProperty(IndexParser.FIELD_USER_DEF_9, i));
      tmpTable.put(IndexParser.FIELD_USER_DEF_10, getConfigProperty(IndexParser.FIELD_USER_DEF_10, i));
      tmpConfTable[i] = tmpTable;
    }
    return tmpConfTable;
  }

  protected static String getConfigProperty(String property, int pos) {
    String value = "";
    //Try to get the specific instant of property value
    String pro = property.concat(separator).concat(new Integer(++pos).toString());
    value = props.getProperty(pro);
    if (value==null || "".equals(value)){
      value = props.getProperty(property);    //get default property if null
    }
    return value;
  }

  public void copySignatureFile(String orgPath, String targetPath) {
    try {
      File inputFile = new File(orgPath);
      java.io.FileInputStream fis = new java.io.FileInputStream(inputFile);
      byte[] content = new byte[1024 * 1024];
      java.io.FileOutputStream fos = new java.io.FileOutputStream(targetPath+"/"+inputFile.getName());

      int bufferSize = 8192;
      byte[] buffer = new byte[bufferSize];
      int length = -1;

      while ( (length = fis.read(buffer, 0, bufferSize)) != -1) {
        fos.write(buffer, 0, length);
      }
      fos.flush();
      fos.close();
      fis.close();
      inputFile.delete();
    } catch (Exception e) {
    }
  }

  public String replaceString(String src, String ori, String tar) {
    String result = null;

    if (src!=null && ori!=null && tar!=null){
      try {
        StringBuffer source = new StringBuffer(src);
        int appearedIndex = 0;
        int oriLength = ori.length();
        int tarLength = tar.length();
        int byPassingIndex = 0;
        // If you got exception here, please check your JDK version. JDK 1.4 is needed for this method.
        while (source.indexOf(ori,byPassingIndex)!=-1) {
          appearedIndex = source.indexOf(ori,byPassingIndex);
          if (ori.equals(source.substring(appearedIndex,appearedIndex+oriLength))) {
            source = source.replace(appearedIndex,appearedIndex+oriLength,tar);
            byPassingIndex = appearedIndex+tarLength;
          } else {
            byPassingIndex = appearedIndex;
          }
        }
        result = source.toString();
      } catch (Exception e) {
        log.error(e, e);
      }
    }
    return result;
  }

}